<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" name="referrer" content="no-referrer">
    <title>轮播图列表</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- import Element UI -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header>
            <h1>
                <img src="../imgs/shark.png">
                CoolShark商城管理系统
                <span v-if="" style="float: right;font-size: 15px;">
                    <a href="JavaScript:void(0)" style="text-decoration: none ;font-family: 楷体">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside>
                <!-- :unique-opened="true":设置子菜单只允许打开一个--->
                <el-menu
                        default-active="5-2"
                        class="el-menu-vertical-demo"
                        :unique-opened="true"
                        @select="handleSelect"
                        @open="handleOpen"
                        @close="handleClose">
                    <el-menu-item index="1">
                        <i class="el-icon-s-home"></i>
                        <span slot="title">首页</span>
                    </el-menu-item>
                    <el-submenu index="2">
                        <template slot="title">
                            <i class="el-icon-user-solid"></i>
                            <span>管理员管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="2-1" v-if="username==='root'">添加管理员</el-menu-item>
                            <el-menu-item index="2-2">管理员列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-s-claim"></i>
                            <span>类别管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="3-1">添加类别</el-menu-item>
                            <el-menu-item index="3-2">类别列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="4">
                        <template slot="title">
                            <i class="el-icon-s-order"></i>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="4-1">添加商品</el-menu-item>
                            <el-menu-item index="4-2">商品列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-picture"></i>
                            <span>轮播图管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="5-1">添加轮播图</el-menu-item>
                            <el-menu-item index="5-2">轮播图列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-menu-item index="6">
                        <i class = "el-icon-user-solid"></i>
                        <span>个人信息</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <el-main>
                <el-table :data="BannerArr">
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column label="轮播图">
                        <template slot-scope="scope">
                            <img :src="scope.row.url" width="200" alt="">
                        </template>
                    </el-table-column>
                    <el-table-column prop="enable" label="是否启用">
                        <template slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.enable"
                                    :active-value="1"
                                    :inactive-value="0"
                                    active-text="启用"
                                    inactive-text="禁用"
                                    active-color="#13ce66"
                                    @change="changeEnable(scope.row)">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button type="danger" icon="el-icon-delete" circle
                                       @click="Delete(scope.row)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogVisible: false,
                dialogFormVisible: false,
                username: localStorage.getItem('username'),
                BannerArr: [],
                banner: {
                    id: '',
                    url: '',
                    enable: ''
                }
            }
        },
        methods: {
            handleSelect(key, keyPath) {
                console.log(v.username);
                if (key==='1'){
                    location.href = 'index.html';
                }else if (key === '2-1') {
                    location.href = 'AddAdmin.html'; // 跳转到添加管理员页面
                } else if (key === '2-2') {
                    location.href = 'AdminList.html'; // 跳转到管理员列表页面
                } else if (key === '3-1') {
                    let name = prompt("请输入类别名称：", "");
                    if (name === null) {
                        return;
                    }
                    if (name.trim() === '') {
                        v.$message({
                            message: '类别名称不能为空！',
                            type: 'warning'
                        });
                    }
                    axios.get('/admin/insertCarousel?name=' + name)
                        .then(function (response) {
                            v.carousel = response.data.data;
                            location.href = 'CarouselList.html'
                        });
                } else if (key === '3-2') {
                    location.href = 'CarouselList.html'; // 跳转到类别列表页面
                } else if (key === '4-1') {
                    location.href = 'AddGoods.html'
                } else if (key === '4-2') {
                    location.href = 'GoodsList.html'
                } else if (key === '5-1') {
                    location.href = 'AddBanner.html'
                }else if (key === '6') {
                    location.href = 'PersonalInfo.html'
                }
            },
            handleOpen(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);

            },
            handleClose(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            handleSuccess(response, file, fileList) {
                // response代表图名
                v.banner.url = response;
                console.log(v.banner.url)
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            changeEnable(banner) {
                console.log(banner.enable);
                v.banner.id = banner.id;
                v.banner.enable = banner.enable;
                v.banner.url = banner.url;
                axios.post('/banner/changeEnable', v.banner)
                    .then(function (response) {
                        console.log('banner的enabel = ' + v.banner.enable)
                        let responseBody = response.data;
                        if (responseBody.state === 20000) {
                            v.$message({
                                message: '修改成功!',
                                type: 'success'
                            });
                            axios.get('/banner/list')
                                .then(function (response) {
                                    let responseBody = response.data;
                                    console.log(responseBody.data)
                                    v.BannerArr = responseBody.data;
                                });
                        } else {
                            v.$message({
                                message: responseBody.message,
                                type: 'error'
                            });
                        }
                    });
            },
            Delete(banner) {
                console.log(banner)
                axios.get('/banner/delete', {params: {id: banner.id}})
                    .then(function (response) {
                        let responseBody = response.data;
                        if (responseBody.state === 20000) {
                            v.$message({
                                message: '删除成功!',
                                type: 'success'
                            });
                            axios.get('/banner/list')
                                .then(function (response) {
                                    let responseBody = response.data;
                                    console.log(responseBody.data)
                                    v.BannerArr = responseBody.data;
                               });
                           }
                    });
            }
        },
        created: function () {
            axios.get('/banner/list')
                .then(function (response) {
                    let responseBody = response.data;
                    console.log(responseBody.data)
                    v.BannerArr = responseBody.data;
                });
        }
    })
</script>
</html>